package io.renren.modules.job.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.dangan.entity.DaDjgllEntity;
import io.renren.modules.dangan.entity.TBpxmEntity;
import io.renren.modules.dangan.entity.TCqzswktEntity;
import io.renren.modules.dangan.service.DaDjgllService;
import io.renren.modules.dangan.service.TBpxmService;
import io.renren.modules.dangan.service.TNzydkwktService;
import io.renren.modules.dangan.service.TNzyxmService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("jsydMapBindTask")
@Slf4j
public class JsydMapBindTask implements ITask {

    @Autowired
    private DaDjgllService daDjgllService;

    @Autowired
    private TBpxmService tBpxmService;
    @Autowired
    private TNzydkwktService tNzydkwktService;
    @Autowired
    private TNzyxmService tNzyxmServicel;
    @Override
    public void run(String params) {
        log.info("开始执行建设用地类的绑定任务");
        int count=0;
        Map<String,Object> pageParams=new HashMap<>();
        pageParams.put(Constant.PAGE,"1");
        pageParams.put(Constant.LIMIT,"100");
        IPage<DaDjgllEntity> page= daDjgllService.page(
                new Query<DaDjgllEntity>().getPage(pageParams),
                new QueryWrapper<DaDjgllEntity>().eq("pflh","E")
        );
        int pageCount= (int) page.getPages();
        log.info("查询结果："+page.getPages()+"页 "+page.getCurrent());
        count=count+bind(page.getRecords());
        while (page.getCurrent()<=page.getPages()){
            pageParams.put(Constant.PAGE,((int)page.getCurrent()+1)+"");
            page= daDjgllService.page(
                    new Query<DaDjgllEntity>().getPage(pageParams),
                    new QueryWrapper<DaDjgllEntity>().eq("pflh","E"));
            count=count+bind(page.getRecords());
            log.info("查询结果："+page.getPages()+"页 "+page.getCurrent()+"更新总数："+count);
        }
        log.info("结束执行建设用地类的绑定任务");

    }

    private int bind(List<DaDjgllEntity> daDjgllEntities){
        if(CollectionUtil.isNotEmpty(daDjgllEntities)){
            List<DaDjgllEntity> updateList=new ArrayList<>();
            daDjgllEntities.forEach(daDjgllEntity -> {
                if(StringUtils.isNotEmpty(daDjgllEntity.getPosition())
                        &&StringUtils.isEmpty(daDjgllEntity.getZdbsm())){
                    String pzwh=daDjgllEntity.getPzwh().trim();
                    TBpxmEntity tBpxmEntity=existMap(daDjgllEntity.getPzwh().trim());
                    if(ObjectUtil.isNotNull(tBpxmEntity)){
                        daDjgllEntity.setXmguid(tBpxmEntity.getXmGuid().toString());
                        updateList.add(daDjgllEntity);
                    }
                }
            });
            if(CollectionUtil.isNotEmpty(updateList)){
                daDjgllService.updateBatchById(updateList,50);
                log.info("更新条数："+updateList.size());
                return updateList.size();
            }
            return 0;
        }
        return 1;
    }


    private TBpxmEntity existMap(String pfwh){
        Map<String,Object> params=new HashMap<>();
        params.put(Constant.LIMIT,"20");
        params.put("pf_wh",pfwh);
        PageUtils page= tBpxmService.queryPage(params);
        if(CollectionUtil.isNotEmpty(page.getList())){
            return (TBpxmEntity) page.getList().get(0);
        }else{
            return null;
        }
    }
}
